package com.ymatou.authapp.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.IpAreaAuthStatusRequestDtoBean;
import com.ymatou.authapp.parameter.LoginAuthBean;
import com.ymatou.authapp.service.IpAreaAuthStatusRequestDtoCall;
import com.ymatou.authapp.service.LoginAuthCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.data.LoginSafeConfiguration;
import com.ymttest.database.mongo.MongoLoginHistoryWapper;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.mongo.YmatouBlacklistUserWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.YMTDateUtil;

/**
*PC端IP区域认证接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_IpAreaAuthStatusRequestDto{
	private static IpAreaAuthStatusRequestDtoBean ipareaauthstatusrequestdtoBean;
	private static IpAreaAuthStatusRequestDtoCall ipareaauthstatusrequestdtoCall;
	private static ULC201406Wapper userWapper;
	private static YmatouBlacklistUserWapper blackLisWapper;
	private static UserSafeWapper authWapper;
	private static final String SOURCETYPE = "mainsite";
	private static MongoLoginHistoryWapper mloginHistoryWapper;
	private static final boolean DELETELOGINHISTORY = true;
	
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("PC端IP区域认证接口");
		
	}

	@Before
	public void caseUp() throws Exception {
		ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
		ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
		userWapper = new ULC201406Wapper();
		blackLisWapper = new YmatouBlacklistUserWapper();
		authWapper=new UserSafeWapper();
		mloginHistoryWapper = new MongoLoginHistoryWapper();
	}

	@AfterClass
	public static void tearDown() throws Throwable {
		Logger.generateResult(ipareaauthstatusrequestdtoCall.getOpurl());
		userWapper.close();
		blackLisWapper.close();
		authWapper.close();
		mloginHistoryWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_001() {
		Logger.start(true,"IP第一次登陆时认证失败，再次认证成功");
		try {
			String ip = "120.90.5.3";//china, shanghai, shanghai
			checkIpLoginHistory(ip,YMTDateUtil.getDateForMongoFormat(-1, 0, 0));
			int userId = 3976;
			ULC201406Bean user = userWapper.getULCInfoByUid(userId);
			userWapper.updatePwdByUid(userId);
			String loginId = user.getlBills().getLoginId();
			
			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(3976, "China", "Shanghai", "Shanghai");
			
			Logger.comment("IP区域登陆，当前设备认证状态是:"+LoginSafeConfiguration.AUTH_PC_OPEN+"(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas("123456a");
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);
			
			Logger.comment("开始第一次认证失败");
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(false);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(false, ipareaauthstatusrequestdtoCall);
			Thread.sleep(1000);
			Map<String, Object> ipAuth = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyNotNull(ipAuth, "认证区域认证信息未被写入数据库");
			Logger.verifyEquals(0, ipAuth.get("AuthStatus"), "验证认证状态为未认证");
			
			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);
			
			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);
			
			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");
			
			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_002() {
		Logger.start(true,"使用Token失效的IP登陆，再次认证成功");
		try {
			String ip = "121.90.5.3";//New Zealand,New Zealand,*
			checkIpLoginHistory(ip,YMTDateUtil.getDateForMongoFormat(-1, 0, 0));
			Logger.comment("修改token最后认证时间为100天前");
			int userId = 4003;
			ULC201406Bean user = userWapper.getULCInfoByUid(userId);
			userWapper.updatePwdByUid(userId);
			String loginId = user.getlBills().getLoginId();
			authWapper.updateTokenUpdateTime(userId, "New Zealand", "New Zealand", "*", YMTDateUtil.getBeforeOrNextDay(-100));
			
			Logger.comment("登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas("123456a");
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);
			
			Logger.comment("第一次认证失败");
			ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(false);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(false, ipareaauthstatusrequestdtoCall);

			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();
			LoginSafeConfiguration.checkLoginStatus(lac2);
			
			Logger.comment("第二次认证成功");
			ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			authWapper.updateTokenUpdateTime(userId, "New Zealand", "New Zealand", "*", YMTDateUtil.getBeforeOrNextDay(-10));
			
			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");
			
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "New Zealand", "New Zealand", "*");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_003() {
		Logger.start(true,"黑名单IP第一次认证失败后，再次认证成功");
		try {
			Logger.comment("添加IP黑名单");
			String ip = "123.90.5.3";//China, Liaoning, Shenyang
			checkIpLoginHistory(ip,YMTDateUtil.getDateForMongoFormat(-1, 0, 0));
			blackLisWapper.saveData(ip, YMTDateUtil.getDateForMongoFormat(-1, 0, 0), YMTDateUtil.getDateForMongoFormat(-1, 0, 0),
					YMTDateUtil.getDateForMongoFormat(1, 0, 0), false, 0);
			
			int userId = 4030;
			ULC201406Bean user = userWapper.getULCInfoByUid(userId);
			userWapper.updatePwdByUid(userId);
			String loginId = user.getlBills().getLoginId();
			
			Logger.comment("登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas("123456a");
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatusWithBlackIp(lac, false, null);
			
			Logger.comment("开始第一次认证失败");
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(false);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(false, ipareaauthstatusrequestdtoCall);
			
			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();
			LoginSafeConfiguration.checkLoginStatusWithBlackIp(lac, false, null);
			
			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);
			
			blackLisWapper.deleteData(ip);
			
			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");
			
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Liaoning", "Shenyang");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_004() {
		Logger.start(true, "按配置类型判断是否让用户执行安全流程:www.ymatou.com,mainsite,app,check_usertype_0");
		try {
			Logger.comment("添加IP黑名单");
			String ip = "123.90.5.3";//China, Liaoning, Shenyang
			checkIpLoginHistory(ip,YMTDateUtil.getDateForMongoFormat(-1, 0, 0));
			blackLisWapper.saveData(ip, YMTDateUtil.getDateForMongoFormat(-1, 0, 0), YMTDateUtil.getDateForMongoFormat(-1, 0, 0),
					YMTDateUtil.getDateForMongoFormat(1, 0, 0), false, 0);
			
			int userId = 1113;
			ULC201406Bean user = userWapper.getULCInfoByUid(userId);
			userWapper.updatePwdByUid(userId);
			String loginId = user.getlBills().getLoginId();
			
			Logger.comment("登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType("www.ymatou.com");
			lab.setPas("123456a");
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatusWithBlackIp(lac, false, null);
						
			Logger.comment("认证成功");
			ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword("123456a");
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();	
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);
			
			blackLisWapper.deleteData(ip);
			
			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");
			
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Liaoning", "Shenyang");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public void checkIpLoginHistory(String ip, String dateTime) throws Exception{
		if(DELETELOGINHISTORY){
			blackLisWapper.deleteData(ip);
			mloginHistoryWapper.deleteByIp(ip, dateTime);
		}
	}
}